package com.d.ding.impl;

import com.d.business.BusinessDataService;
import com.d.business.domain.dto.DingProcessConfDTO;
import com.d.client.DingClient;
import com.d.client.dto.DingProcessInstanceResultDTO;
import com.d.client.dto.DingProcessInstanceWirteDTO;
import com.d.ding.DingApproveService;
import com.d.business.domain.dto.AssetPurchaseDTO;
import com.d.ydfactory.domain.ApproveStartResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

/**
 * 此类需要添加业务代码
 * 可将此类优化成公共类进行配置
 * 业务类
 */
@Service
public class DingApproveServiceImpl extends DingBaseImpl implements DingApproveService {

    private static final Logger log = LoggerFactory.getLogger(DingApproveServiceImpl.class);
    @Autowired
    private DingClient dingClient;
//    @Autowired
//    private BAdminInfoService bAdminInfoService;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public ApproveStartResult start(Integer busId,DingProcessConfDTO conf) {
        ApproveStartResult result = new ApproveStartResult();
        try {
            //获取业务数据
//            AssetPurchaseDTO assetPurchaseDTO = assetPurchaseConsumer.getDataById(bussId);

            //不同的调用方式
//            FeignInvoker feignInvoker =new FeignInvoker(applicationContext);
//            AssetPurchaseDTO assetPurchaseDTO = (AssetPurchaseDTO) feignInvoker.invokeFeignMethod(
//                    GetBusinessDataConsumer.class,
//                    "getDataById",
//                    new Class[]{Integer.class},
//                    bussId
//            );

            //不同的调用方法
//            AssetPurchaseConsumer proxy = applicationContext.getBean(AssetPurchaseConsumer.class);
//
//            Method method = AssetPurchaseConsumer.class.getMethod("getDataById", Integer.class);
//            AssetPurchaseDTO assetPurchaseDTO = (AssetPurchaseDTO) method.invoke(proxy, bussId);

            //以上代码调用feign interface 方式 在AssetPurchaseConsumer类中增加对应的业务类将method写活即可
            //以下是调用java interface类方式，在DingAssetPurchaseApproveImpl中增加不同的实现类将写活即可getMethod
            Class<?> serviceClass = Class.forName(conf.getQueryDataClass());
//        Class<?> serviceClass = Class.forName(DingFromConstants.dingProcessQueryDataServiceJoin.get(insert.getType()));
            BusinessDataService service = (BusinessDataService) serviceClass.getDeclaredConstructor().newInstance();
            Method processMethod = serviceClass.getMethod(conf.getQueryDataMethod(), Integer.class);
            AssetPurchaseDTO assetPurchaseDTO = (AssetPurchaseDTO) processMethod.invoke(service, busId);

            //获取ding用户信息，数据库结构的原因，根据业务需求改变获取方式
//            BAdminInfoDO adminInfo = bAdminInfoService.get(assetPurchaseDTO.getAdminId());
//            if (adminInfo == null || adminInfo.getRUserId() == null || "".equals(adminInfo.getRUserId())) {
//                result.setCode("-1");
//                result.setMsg("未设置钉钉账号！");
//                return result;
//            }

            //组装钉钉数据结构
            DingProcessInstanceWirteDTO dto = dingFrom(assetPurchaseDTO,new DingProcessConfDTO());
//            DingProcessInstanceResultDTO resultDTO = new DingProcessInstanceResultDTO();
//            resultDTO.setInstanceId(System.currentTimeMillis() + "");
            DingProcessInstanceResultDTO r = dingClient.createProcess(dto);
            result.setCode("-1");
            if (r != null && r.getInstanceId() != null) {
                result.setInstanceId(r.getInstanceId());
                result.setCode("0");
                result.setMsg("成功");
                result.setBusinessNo(assetPurchaseDTO.getBatchId() + "");
                result.setStaffId(dto.getOriginatorUserId());
            }
            return result;
        } catch (Exception e) {
//            log.error(e.toString());
//            result.setCode("-1");
//            result.setMsg(e.getMessage());
//            return result;
            throw new RuntimeException(e);
        }
    }

    /**
     * 更新系统状态数据
     * 具体可根据业务优化
     *
     * @param busId
     * @param status
     * @param adminId
     * @param adminName
     * @return
     */
    @Override
    public int updateStatus(Integer type,String busId, Integer status, Integer adminId, String adminName,DingProcessConfDTO conf) throws Exception {
        Class<?> serviceClass = Class.forName(conf.getUpdateDataClass());
//        Class<?> serviceClass = Class.forName(DingFromConstants.dingProcessQueryDataServiceJoin.get(insert.getType()));
        BusinessDataService service = (BusinessDataService) serviceClass.getDeclaredConstructor().newInstance();
        Method processMethod = serviceClass.getMethod(conf.getUpdateDataMethod(), Integer.class,Integer.class,Integer.class,String.class);
        Integer result = (Integer) processMethod.invoke(service, busId,status,adminId,adminName);
        return result;
    }
}
